package omg.loot;

import java.awt.Dimension;
import java.awt.event.HierarchyBoundsAdapter;
import java.awt.event.HierarchyEvent;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.swing.JFrame;

public class Main extends JFrame {
   private static final long serialVersionUID = 1L;
   public static final int WIDTH = 1024;
   public static final int HEIGHT = 768;
   public static final int CENTERX = WIDTH/2;
   public static final int CENTERY = HEIGHT/2;
   
   protected Thread thread;
   protected GameRunnable game;
   protected Renderer renderer;
   
   public static void main(final String[] args) {
      new Main();      
   }
   
   public Main() {  
      super(":omgloot Tower Defense");
      game = new GameRunnable();
      final GLProfile glp = GLProfile.getDefault();
      final GLCapabilities caps = new GLCapabilities(glp);
      final Renderer renderer = new Renderer(caps, game);   
      renderer.addGLEventListener(renderer);
      game.renderer = renderer;
      
      renderer.setSize(WIDTH, HEIGHT);      
      add(renderer);
      pack(); //Set the window size to wrap the contents 
      setVisible(true);
      
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      getContentPane().addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
          @Override
          public void ancestorResized(HierarchyEvent e) {
             Dimension size = e.getComponent().getSize();
             Shared.scaleX = (float)WIDTH/(float)size.width;
             Shared.scaleY = (float)HEIGHT/(float)size.height;
          }          
      });
      
      renderer.addKeyListener(game.key);
      renderer.addMouseListener(game.mouse);
      renderer.addMouseWheelListener(game.mouse);
      renderer.addMouseMotionListener(game.mouse);      
      
      thread = new Thread(game);
      thread.start();
   }
}